<template>
  <div class="login_container">
    <!-- 登录盒子  -->
    <div class="login_box">
      <img src="../assets/image/logo.png" alt class="avatar_box" />
      <!-- 表单区域 -->
      <el-form
        class="login_form"
        label-width="70px"
        :model="loginform"
        ref="form"
        :rules="ruleform"
      >
        <el-form-item label="用户名" prop="username">
          <el-input clearable v-model="loginform.username">
            <i slot="prefix" class="el-input__icon el-icon-user"></i>
          </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input clearable v-model="loginform.password" show-password>
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="warning" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginform: {
        username: "admin",
        password: "123456",
      },
      ruleform: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    login() {
      // 5.登陆事件
      //   5.1验证表单数据是否符合要求validate
      this.$refs.form.validate(async (valid) => {
        if (!valid) return; // 验证不通过跳出
        // 触发action
        this.$store.dispatch("login_admin", this.loginform);
      });
    },
    reset() {
      this.$refs.form.resetFields();
    },
  },
};
// 补充1
// ref : 获取原生dom节点组成的对象，也可可获取组件
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b5b6b;
  height: 100vh;
}
.login_box {
  width: 450px;
  height: 300px;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>